package homework.hw1;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author Administrator
 * @Date 2020/5/21 13:45
 * 定义--Servlet,5秒后自动跳转到另一html文件，
 * 或者点击超链接跳转
 **/
@WebServlet("/refreshDemo")
public class RefreshDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*1.页面上显示这些文字信息：输出流写数据
        2.5秒后自动跳转：设置响应头
        3.点击链接跳转：超链接
         */
        /*响应乱码情况：设置服务端的编码为utf-8,通知浏览器用utf-8编码区解析数据展示*/
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        pw.write("支付成功，5秒自动跳转...");
        pw.write("<a href='http://localhost:8080/dome/refreshDemo.html'>若未跳转，点击这里</a>");
        //设置响应头，5秒后自动跳转
        resp.setHeader("Refresh","5;URL=http://localhost:8080/dome/refreshDemo.html");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
